package com.paipaideli.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.paipaideli.BuildConfig;
import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterFragment;
import com.paipaideli.common.http.Api;
import com.paipaideli.common.pullrefresh.TStatusView;
import com.paipaideli.common.utils.ImageUtil;
import com.paipaideli.common.utils.ToastUtil;
import com.paipaideli.common.views.CircleImageView;
import com.paipaideli.ui.mine.invitation.InvitationActivity;
import com.paipaideli.ui.mine.address.AddAddressActivity;
import com.paipaideli.ui.mine.head.AssetsActivity;
import com.paipaideli.ui.mine.head.MyInfoActivity;
import com.paipaideli.ui.mine.head.ProfitActivity;
import com.paipaideli.ui.mine.help.HelpCenterActivity;
import com.paipaideli.ui.mine.order.OrderActivity;
import com.paipaideli.ui.mine.safety.SafetyCenterActivity;
import com.paipaideli.ui.webView.WebAcyivity;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MineFragment extends BasePresenterFragment implements View.OnClickListener {

    Unbinder unbinder;
    @BindView(R.id.top_back)
    RelativeLayout top_back;
    @BindView(R.id.top_title)
    TextView top_title;
    @BindView(R.id.lin_mine_tjyl)
    RelativeLayout lin_mine_tjyl;
    @BindView(R.id.lin_mine_aqzx)
    RelativeLayout lin_mine_aqzx;
    @BindView(R.id.lin_mine_bzzx)
    RelativeLayout lin_mine_bzzx;
    @BindView(R.id.lin_mine_shdz)
    RelativeLayout lin_mine_shdz;
    @BindView(R.id.lin_mine_gywm)
    RelativeLayout lin_mine_gywm;

    @BindView(R.id.lin_order_dfk)
    LinearLayout lin_order_dfk;
    @BindView(R.id.lin_order_dfh)
    LinearLayout lin_order_dfh;
    @BindView(R.id.lin_order_dsh)
    LinearLayout lin_order_dsh;
    @BindView(R.id.lin_order_ysx)
    LinearLayout lin_order_ysx;

    @BindView(R.id.mine_image_icon)
    CircleImageView mine_image_icon;
    @BindView(R.id.mine_text_name)
    TextView mine_text_name;
    @BindView(R.id.mine_text_one)
    TextView mine_text_one;
    @BindView(R.id.mine_text_two)
    TextView mine_text_two;
    @BindView(R.id.lin_mine_ljsy)
    LinearLayout lin_mine_ljsy;
    @BindView(R.id.lin_mine_wdzc)
    LinearLayout lin_mine_wdzc;
    @BindView(R.id.lin_mine_wddd)
    LinearLayout lin_mine_wddd;
    @BindView(R.id.rel_image_header)
    RelativeLayout rel_image_header;

    @BindView(R.id.image_my_sm)
    ImageView image_my_sm;
    @BindView(R.id.tv_image_sm)
    TextView tv_image_sm;
    @BindView(R.id.bg_statusview)
    TStatusView bg_statusview;

    @BindView(R.id.app_version)
    TextView app_version;

    private String certificationStatu;
    private String avatar;
    private String username;

    public static MineFragment newInstance() {
        MineFragment fragment = new MineFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initDate();
    }

    @Override
    public void initView() {
        top_title.setText("我的");
        app_version.setText("当前版本："+BuildConfig.VERSION_NAME);
        top_back.setVisibility(View.GONE);
        lin_mine_tjyl.setOnClickListener(this::onClick);
        lin_mine_aqzx.setOnClickListener(this::onClick);
        lin_mine_bzzx.setOnClickListener(this::onClick);
        lin_mine_shdz.setOnClickListener(this::onClick);
        lin_mine_gywm.setOnClickListener(this::onClick);
        mine_image_icon.setOnClickListener(this::onClick);
        lin_mine_ljsy.setOnClickListener(this::onClick);
        lin_mine_wdzc.setOnClickListener(this::onClick);
        lin_mine_wddd.setOnClickListener(this::onClick);

        lin_order_dfk.setOnClickListener(this::onClick);
        lin_order_dfh.setOnClickListener(this::onClick);
        lin_order_dsh.setOnClickListener(this::onClick);
        lin_order_ysx.setOnClickListener(this::onClick);
        rel_image_header.setOnClickListener(this::onClick);

        initDate();
    }

    private void initDate() {
        JSONObject requestData = new JSONObject();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestData.toString());
        Api.getInstanceGson().mine(body)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(request -> {
                    if (request.code.equals("200")) {
                        mine_text_name.setText(request.data.username);
                        mine_text_one.setText("￥" + request.data.bounty);
                        mine_text_two.setText("￥" + request.data.remainder + "");
                        avatar = request.data.avatar;
                        username = request.data.username;
                        ImageUtil.displayImage(request.data.avatar, mine_image_icon);
                        certificationStatu = request.data.certification;
                        if (certificationStatu.equals("1")) {
                            image_my_sm.setBackgroundResource(R.mipmap.icon_mine_ysm);
                            tv_image_sm.setText("已实名");
                        } else {
                            image_my_sm.setBackgroundResource(R.mipmap.icon_mine_wsm);
                            tv_image_sm.setText("未实名");
                        }
                    } else {
                        ToastUtil.show(request.msg);
                    }
                }, throwable -> {
                });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.lin_mine_tjyl:
                intent = new Intent(getActivity(), InvitationActivity.class);
                intent.putExtra("image", avatar);
                intent.putExtra("name", username);
                startActivity(intent);
                break;
            case R.id.lin_mine_wddd:
                intent = new Intent(getActivity(), OrderActivity.class);
                intent.putExtra("whichOrder", "1");
                startActivity(intent);
                break;
            case R.id.lin_order_dfk:
                intent = new Intent(getActivity(), OrderActivity.class);
                intent.putExtra("whichOrder", "2");
                startActivity(intent);
                break;
            case R.id.lin_order_dfh:
                intent = new Intent(getActivity(), OrderActivity.class);
                intent.putExtra("whichOrder", "3");
                startActivity(intent);
                break;
            case R.id.lin_order_dsh:
                intent = new Intent(getActivity(), OrderActivity.class);
                intent.putExtra("whichOrder", "4");
                startActivity(intent);
                break;
            case R.id.lin_order_ysx:
                intent = new Intent(getActivity(), OrderActivity.class);
                intent.putExtra("whichOrder", "5");
                startActivity(intent);
                break;
            case R.id.lin_mine_aqzx:
                intent = new Intent(getActivity(), SafetyCenterActivity.class);
                intent.putExtra("certificationStatu", certificationStatu);
                startActivity(intent);
                break;
            case R.id.lin_mine_bzzx:
                intent = new Intent(getActivity(), HelpCenterActivity.class);
                startActivity(intent);
                break;
            case R.id.lin_mine_shdz:
                intent = new Intent(getActivity(), AddAddressActivity.class);
                intent.putExtra("which", "2");
                startActivity(intent);
                break;
            case R.id.lin_mine_gywm:
                intent = new Intent(getActivity(), WebAcyivity.class);
                intent.putExtra("url", BuildConfig.h5 + "aboutUs.html");
                intent.putExtra("title", "关于我们");
                startActivity(intent);
                break;
            case R.id.mine_image_icon:
                intent = new Intent(getActivity(), MyInfoActivity.class);
                intent.putExtra("name", username);
                intent.putExtra("image", avatar);
                startActivity(intent);
                break;
            case R.id.lin_mine_ljsy:
                intent = new Intent(getActivity(), ProfitActivity.class);
                startActivity(intent);
                break;
            case R.id.lin_mine_wdzc:
                intent = new Intent(getActivity(), AssetsActivity.class);
                intent.putExtra("certificationStatu", certificationStatu);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            initDate();
        }
    }

}
